let href= location.href
console.log(href);
function getUrlParams(url){
    let reg =/^[^\s]+\?([^&\s\d]+=[^\s]+)*/
    let match =reg.exec(url)
    
    match= match[1];
    // console.log(match);
    if (!match){
        console.warn("没有匹配到参数")
        return null
    }
    // 解码
    let qureyString =decodeURI(match);
    // console.log(qureyString);
    // 切割qureyString成为数组；
    let qureyArr= qureyString.split('&');
    // console.log(qureyArr);

    let queryParamsObj={};
    qureyArr.forEach(item=>{
        let itemArr = item.split("=")
        queryParamsObj[itemArr[0]]=itemArr[1]
    })
    // console.log(queryParamsObj);
    return queryParamsObj;
}
// 获取url参数
let qr=getUrlParams(href);
console.log(qr);

$.ajax({
    url:"http://chst.vip:1234/api/getcategorybyid",
    data:{
        categoryid:qr.categoryid
    }
}).then(res=>{
    renderCategory(res.result)
}).then(function(){
    $.ajax({
        url:"http://chst.vip:1234/api/getproduct",
        data:{
            categoryid:qr.categoryid,
            productid:qr.productid
        }
    }).then(res=>{
        renderProduct(res.result);
        rendermessage(res.result)

    })
}).then(function(){
    $.ajax({
        url:"http://chst.vip:1234/api/getproductcom",
        data:{
            
            productid:qr.productid
        }
    }).then(res=>{
        rendercomment(res.result);
       console.log(res);

    })
})

function renderCategory(data=[]) {
    let html=`
        <a href="prise_parity_goods.html?categoryid=${data[0].categoryId}">${data[0].category}&gt</a>   
    `
    
    $(html).insertAfter($("#index"))
    
    
}
function renderProduct(data=[]) {
    let name = data[0].productName.split(" ")[0];
    // console.log(name);
    let html=`
        <a href="#">${name}</a>   
    `
    
    $(html).insertBefore($("#button"))
    
    
}
function rendermessage(data=[]) {
    // let name = 0;
    // console.log(name);
    let html=`
    <div class="pic">
    ${data[0].productImg}           
    </div>
    <div class="title">
    ${data[0].productName}  
    </div>
    <div class="other"><span>当日最低价</span><span>优选评论</span></div>
    ${data[0].bjShop}
    `
    
    $(".contain").append(html)
    
    
}
function rendercomment(data=[]) {
    let html="";
    
   data.forEach(item=>{
    html+=`
    <div class="comment">
                    <div class="top clear_float">
                        <sapn class="comment_name">${item.comName}</sapn>
                        <span class="comment_date">${item.comTime}</span>

                    </div>
                    <div class="content">
                        ${item.comContent}
                    </div>
                    <div class="from clear_float">
                        <span>${item.comFrom}</span>
                    </div>
                </div>
    `
   })
    
    $(".comment_contain").append(html)
    
    
}